<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/26
  Time: 16:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>注册新用户</title>
</head>

<body >
 <%
    request.setCharacterEncoding("UTF-8");
    String user_id = request.getParameter("userid");
    String user_name = request.getParameter("username");
    String user_password = request.getParameter("userpassword");
    String user_marks = request.getParameter("usermarks");

//    System.out.println(user_id + user_name + user_password);

    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null;

    Class.forName("com.mysql.cj.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/talknewsdatabase";
    String user = "root";
    String password = "123456";
    conn = DriverManager.getConnection(url, user, password);

    stat = conn.createStatement();
    String sql = "insert into user(user_id,user_name,user_password,user_marks) values('" + user_id + "','" + user_name + "','" + user_password + "','"+user_marks+ "')";
    stat.executeUpdate(sql);
    rs = stat.executeQuery("select * from user");

%>
 
<%
    if (rs.next()) {
        out.print("<script>alert('成功输入！');</script>");
    } else {
        out.print("<script>alert('输入失败！');</script>");
    }
%>

  <%
    if (rs != null) {
        rs.close();
        rs = null;
    }
    if (stat != null) {
        stat.close();
        stat = null;
    }
    if (conn != null) {
        conn.close();
        conn = null;
    }
%>     

<script>window.location='http://localhost:8080/index.html';</script>
      
</body>
</html>
